Skip to content

【Inference Optimize】optimize DeepSeek_v3 #3349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

chang-wenbin
Copy link
Collaborator

@chang-wenbin chang-wenbin commented Aug 12, 2025

1、Eliminate redundant calculations

      fmha_out = None
        # NOTE: (changwenbin) Bring out the public calculation in PD MIX to avoid repeated calculation.
        query = self.q_a_proj(hidden_states)
        query = self.q_a_layernorm(query)
        query = self.q_b_proj(query)
        query = query.reshape([-1, self.num_attention_heads_tp, self.qk_head_dim])
        query_nope, query_pe = query.split([self.qk_nope_head_dim, self.qk_rope_head_dim], axis=-1)
        compressed_kv = self.kv_a_proj_with_mqa(hidden_states)
        compressed_kv, key_pe = compressed_kv.split([self.kv_lora_rank, self.qk_rope_head_dim], axis=-1)
        key_pe = key_pe.reshape([-1, 1, self.qk_rope_head_dim])
        compressed_kv = self.kv_a_layernorm(compressed_kv)
        query_pe, key_pe = self.rotary_emb(position_ids, query_pe, key_pe)

2、Enable FA3 on the H card to execute the encoder
you need export FLAGS_flash_attn_version=3

Copy link

paddle-bot bot commented Aug 12, 2025

Thanks for your contribution!

q,
k,
v,
forward_meta.cu_seqlens_q,
forward_meta.cu_seqlens_k,
metadata.max_enc_len_this_time,
metadata.max_enc_len_this_time,
self.attn_softmax_scale,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.attn_softmax_scale 这个参数,这样传入的话,会用你上面创建的{"scale": self.head_dim**-0.5, 吗?
self.attn_softmax_scale 看之前的代码,是经过self.attn_softmax_scale * mscale * mscale计算出来的

self.attn_softmax_scale,
causal=True,
training=False,
max_seqlen_q=forward_meta.max_len_tensor_cpu[0],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改之后,测过之前的flash_attn_unpadded没, 输出是否有变化


if forward_meta.max_len_tensor_cpu[2]: # max_dec_len_this_time
# NOTE: (changwenbin) We will take the public part
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

take 如何理解, 是准备表达will solove public part,还是会计算public part

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants